package com.toycloud.common.math;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.NumberFormat;
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class PercentFormat {

    /**
     * integer转为百分比
     */
    public static String formatPercent (Integer a,Integer b){
        if(a!=0){
            BigDecimal result = new BigDecimal(a).divide(new BigDecimal(b), 4, RoundingMode.HALF_UP);
            NumberFormat percentFormat = NumberFormat.getPercentInstance();
            percentFormat.setMinimumFractionDigits(2); // 设置百分比的最小小数位数为2
            String percentage = percentFormat.format(result);
            return percentage;
        }else{
            return "0%";
        }

    }

}
